using System;
using System.Collections.Generic;
using TickSharp.Core.Enum;
using TickSharp.Core.Infrastructure;
using TickSharp.Core.Infrastructure.Repositories;
using TickSharp.Core.Model;
using Xunit;

namespace TickSharp.Tests.UnitTests.Infrastructure
{
    public class EntryRepositoryTester
    {
        [Fact]
        public void FilterUrlReturnsEmptyStringWhenEmptyListOfFiltersPassed()
        {
            const string notUsed = "asdf";
            var entryRepository = new EntryRepository(new ApiConnectionInfo(notUsed, notUsed, notUsed));

            string filterUrl = entryRepository.FilterUrl(new List<EntryFilter>());

            Assert.Equal(string.Empty, filterUrl);
        }

        [Fact]
        public void FilterUrlThrowsArgumentNullExceptionWhenNullListOfFiltersPassed()
        {
            const string notUsed = "asdf";
            var entryRepository = new EntryRepository(new ApiConnectionInfo(notUsed, notUsed, notUsed));

            Assert.Throws(typeof(ArgumentNullException), delegate { entryRepository.FilterUrl(null); });
        }

        [Fact]
        public void FilterUrlReturnsExpectedFilterWhenProjectFilterGiven()
        {
            const string notUsed = "asdf";
            var entryRepository = new EntryRepository(new ApiConnectionInfo(notUsed, notUsed, notUsed));
            const string apiName = "project_id";
            const int projectId = 20;            
            
            EntryFilter filter = EntryFilter.Create(EntryFilterType.ProjectId, projectId);
            IEnumerable<EntryFilter> entryFilters = new []{ filter };
            string expectedFilterString = string.Format("&{0}={1}", apiName, projectId);

            string filterString = entryRepository.FilterUrl(entryFilters);

            Assert.Equal(expectedFilterString, filterString);
        }

        [Fact]
        public void FilterUrlReturnsExpectedFilterWhenManyProjectFiltersGiven()
        {
            const string notUsed = "asdf";
            var entryRepository = new EntryRepository(new ApiConnectionInfo(notUsed, notUsed, notUsed));
            const string apiName = "project_id";
            const int projectId = 20;

            EntryFilter filter = EntryFilter.Create(EntryFilterType.ProjectId, projectId);
            IEnumerable<EntryFilter> entryFilters = new[] { filter, filter };
            string expectedFilterString = string.Format("&{0}={1}", apiName, projectId);
            expectedFilterString += expectedFilterString;

            string filterString = entryRepository.FilterUrl(entryFilters);

            Assert.Equal(expectedFilterString, filterString);
        }
    }
}